import akka.actor.Actors;
import akka.actor.UntypedActor;

/**
 * Receives messages containing lines from files
 * Terminates after recieving one message per file 
 */
public class CollectionActor extends UntypedActor {
	private int fileCount;

	@Override
	public void onReceive(Object message) throws Exception {
		if (message instanceof FileCount) {
			this.fileCount = ((FileCount)message).getCount();
		} else if (message instanceof Found) {
			Found found = (Found)message;
			fileCount--;

			// Print message
			System.out.println("File: " + found.getFileName());
			System.out.println("--Matching lines--");
			for (String line : found.getMatchingLines()) {
				System.out.println("\t" + line);
			}

			// Terminate after all messages have been received
			if (fileCount <= 0)	Actors.registry().shutdownAll();
		} 
	}
}
